package cn.wolfcode.Controller;


import cn.wolfcode.domain.Notice;
import cn.wolfcode.meiju.NoticeStatusEnum;
import cn.wolfcode.qo.NoticeObject;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.jsonResult;
import cn.wolfcode.util.RequirePermission;
import cn.wolfcode.util.UserContext;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;


    @RequirePermission(name = "公告单页面",experssion = "notice:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") NoticeObject qo){
        NoticeStatusEnum[] values = NoticeStatusEnum.values();
        model.addAttribute("values", values);


        if (!UserContext.getCurrentEmployee().isAdmin()){ //如果是管理员
            qo.setPublishStatus(1);
        }
        PageInfo<Notice> pageInfo = noticeService.query(qo);
        model.addAttribute("pageInfo", pageInfo);


        //获取该用户还没读的信息
        int noRead = noticeService.selectNoRead(UserContext.getCurrentEmployee().getId());
        model.addAttribute("noRead",noRead);
        noticeService.updateNotice(UserContext.getCurrentEmployee().getId());
        return "notice/list";
    }



    @RequirePermission(name = "查看公告内容",experssion = "notice:show")
    @RequestMapping("/show")
    public String show(Long id,Model model){
        Notice notice;
        if (id != null){
            notice = noticeService.get(id);
            model.addAttribute("notice",notice);
        }
        noticeService.UpdateReadStatus(id, UserContext.getCurrentEmployee().getId(), 1L);
        return "notice/show";
    }

    @RequirePermission(name = "查看未读条数",experssion = "notice:showNoRead")
    @RequestMapping("/showNoRead")
    public String showNoRead(Model model){
        int noRead = noticeService.selectNoRead(UserContext.getCurrentEmployee().getId());
        model.addAttribute("noRead",noRead);
        return "common/navbar";
    }


    @RequirePermission(name = "发布公告",experssion = "notice:publish")
    @RequestMapping("/publish")
    public String publish(Long noticeId,Model model,boolean publish_status ){
         noticeService.updatePublish(noticeId, publish_status);
        //更新公告内容
        noticeService.updateNotice(UserContext.getCurrentEmployee().getId());
        return "redirect:/notice/list";
    }

    @RequestMapping("/delete")
@RequirePermission(name = "公告单页面删除",experssion = "notice:delete")
    @ResponseBody
    public jsonResult delete(Long id){
        if (id != null) {
            noticeService.delete(id);
        }
        return new jsonResult();
    }


    @RequestMapping("/saveOrUpdate")
@RequirePermission(name = "公告单页面编辑",experssion = "notice:saveOrUpdate")
    public String saveOrUpdate(Notice notice){
        if (notice.getId() != null) {
            noticeService.update(notice);
        }else {
            noticeService.save(notice);
        }
        return "redirect:/notice/list";
    }
}
